//  ====================================================================
//  Copyright (C) 2007-2010  Jean-Philippe GOLAY

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//  =====================================================================

// GLOBAL VAR
var itemsCaddy=new Array();

function Item(id, reference, designation, quantity, price, tax){

    this.id = id;
    this.reference = reference;
    this.designation = designation;
    this.quantity = quantity;
    this.price = price;
    this.tax = tax;

}

function addCaddy(id){
    
    it = new Item( id, 
                   document.getElementById("reference").value,
                   document.getElementById("designation").value,
                   1,
                   document.getElementById("price").value,
                   document.getElementById("tax").value );

    itemsCaddy.push(it);
    showCaddy();

}

function removeCaddy(id){

    var pass = 0;
    for( i=0; i < itemsCaddy.length; i++ ){
        if( itemsCaddy[i].id == id && pass == 0 ){
            itemsCaddy[i] = 0;
            pass = 1;
	}
    }
    showCaddy();

}

function payCaddy(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    var countItem = 0;
    for( i in itemsCaddy  ){
      if( itemsCaddy[i] != 0 ){
        myValues += "&product_id"+i+"="+itemsCaddy[i].id;
        countItem += 1;
      }
    }
    if( countItem > 0  ){
      myValues += "&num="+countItem;
      sendXmlHttpRequest( myValues, "getProductPayment.py", -1, "area_store" );
      cleanCaddy();
    }
  }else{
    return;
  }

}

function cleanCaddy(){

    itemsCaddy=new Array();
    showCaddy();

}

function showCaddy(){
    
    var html = "<table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td class='text_left'><b>R&eacute;f&eacute;rence</b></td><td class='text_left'><b>D&eacute;signation</b></td><td><b>Quantit&eacute;</b></td><td class='text_right'><b>Taxes</b></td><td class='text_right'><b>Prix (HT)</b></td><td>&nbsp;</td></tr>";
    var totalPrice = 0;
    var totalTax = 0;

    for( item in itemsCaddy ){
        if( itemsCaddy[item] != 0 ){
            totalPrice += parseFloat(itemsCaddy[item].price);
            totalTax += ((parseFloat(itemsCaddy[item].price)/100)*parseFloat(itemsCaddy[item].tax))
            html += "<tr><td class='text_left'>"+itemsCaddy[item].reference+"</td><td class='text_left'>"+itemsCaddy[item].designation+"</td><td>"+itemsCaddy[item].quantity+"</td><td class='text_right'>"+itemsCaddy[item].tax+"</td><td class='text_right'>"+itemsCaddy[item].price+"&euro;</td><td><a id='"+itemsCaddy[item].id+"' href='#' onclick='removeCaddy(this.id)'><img src='/img/undo_icon.gif' title='Supprimer' alt='supprimer' height='16' width='16'/></a></td></tr>";
	}
    }
    if( totalPrice == 0 ){
        html = "&nbsp;";
    }else{
        html += "<tr><td colspan='4' class='text_right'>Total HT : </td><td class='text_right'>"+totalPrice.toFixed(2)+"&euro;</td><td></td></tr><tr><td colspan='4' class='text_right'>Total Taxes : </td><td class='text_right'>"+totalTax.toFixed(2)+"&euro;</td><td></td></tr><tr><td colspan='4' class='text_right'><b>Total TTC : </b></td><td class='text_right'><b>"+(totalPrice + totalTax).toFixed(2)+"&euro;</b></td><td><a href='#' onclick='cleanCaddy()'><img src='/img/undo_icon.gif' title='Supprimer tout' alt='supprimer' height='16' width='16'/></a></td></tr></table><input type='button' value='Payer' onclick='payCaddy()' title='Confirmer la commande'/><br/><br/><br/>";
    }
    document.getElementById("caddy").innerHTML=html;

}

function confirmPayment(){

  if(document.getElementById){
    if(document.getElementById("name").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un nom !</b>";
      getFocus("name");
      return;
    }
    if(document.getElementById("address").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une adresse !</b>";
      getFocus("address");
      return;
    }
    if(document.getElementById("email").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une adresse email !</b>";
      getFocus("email");
      return;
    }
    if(document.getElementById("phone").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un num&eacute;ro de t&eacute;l&eacute;phone !</b>";
      getFocus("phone");
      return;
    }
    if(document.getElementById("address").value.length > 150){
      document.getElementById("msg_box").innerHTML="<b>Adresse trop longue (maxi 150 caract.) !</b>";
      getFocus("address");
      return;
    }
    if(document.getElementById("note").value.length > 250){
      document.getElementById("msg_box").innerHTML="<b>Commentaire trop long (maxi 250 caract.) !</b>";
      getFocus("note");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&bill_id="+document.getElementById("sale_id").value;
    myValues += "&name="+encodeURIComponent(document.getElementById("name").value);
    myValues += "&address="+encodeURIComponent(document.getElementById("address").value);
    myValues += "&email="+encodeURIComponent(document.getElementById("email").value);
    myValues += "&phone="+encodeURIComponent(document.getElementById("phone").value);
    myValues += "&note="+encodeURIComponent(document.getElementById("note").value);

    sendXmlHttpRequest( myValues, "getConfirmPayment.py", -1, "confirm_payment" );
  }else{
    return;
  }

}

function sendXmlHttpRequest(myValues, target, menuId, id){

  if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari
    var xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById(id).innerHTML=xmlhttp.responseText;
      if (menuId != -1){
        selectedNav(menuId);
      }
      document.getElementsByTagName('body')[0].style.cursor = 'pointer';
    }
  }

  xmlhttp.open("POST", target, true);
  // Send the proper header information along with the request
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", myValues.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(myValues);
  document.getElementsByTagName('body')[0].style.cursor = 'wait';
}

function overImg(img, id) {
  document.getElementById(id).style.backgroundImage = "url(skin/img/"+img+"_over.png)";
}

function outImg(img, id) {
  document.getElementById(id).style.backgroundImage = "url(skin/img/"+img+".png)";
}

function goPage(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&page_id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getPage.py", id, "page_content"); 

}

function selectedNav(id){

  var  listTag = document.getElementsByTagName("a");

  for ( i=0; i<listTag.length; i+=1 ){
      if ( listTag[i].className == "selected" ){
          listTag[i].className="";
      }
  }
  document.getElementById(id).className="selected";

}

function login(){

  if(document.getElementById){
    document.getElementById("page_content").innerHTML="<br/><br/><table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td colspan='2'>LOGIN</td></tr><tr><td colspan='2'><div id='msg_box'>&nbsp;</div></td></tr><tr><td>Nom d'utilisateur :</td><td><input type='text' id='username'></td></tr><tr><td>Mot de passe :</td><td><input type='password' id='passwd'></td></tr><tr><td colspan='2'><input type='button' onclick='connexion()' value='Envoyer'></td></tr></table><br/><br/>";
    getFocus("username");
  }

}

function connexion(){

  if(document.getElementById){
    if(document.getElementById("username").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un nom d'utilisateur !</b>";
      getFocus("username");
      return;
    }
    if(document.getElementById("passwd").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un mot de passe !</b>";
      getFocus("passwd");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&username="+document.getElementById("username").value;
    myValues += "&passwd="+document.getElementById("passwd").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getAdmin.py", -1, "page_content");

}

function modifyPage(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&page_id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetPage.py", id, "page_content");

}

function updatePage(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&page_id="+document.getElementById("page_id").value;
    myValues += "&page_title="+encodeURIComponent(document.getElementById("page_title").value);
    myValues += "&nav_id="+document.getElementById("nav_id").value;
    myValues += "&nav_name="+encodeURIComponent(document.getElementById("nav_name").value);
    myValues += "&nav_title="+encodeURIComponent(document.getElementById("nav_title").value);
    myValues += "&nav_parent_id="+document.getElementById("nav_parent").value;
    myValues += "&nav_position="+document.getElementById("nav_position").value;
    myValues += "&nav_onclick="+encodeURIComponent(document.getElementById("nav_onclick").value);
    myValues += "&nav_selected="+document.getElementById("nav_selected").value;
    myValues += "&page_content="+encodeURIComponent(document.getElementById("pg_content").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updatePage.py", -1, "page_content");

}

function deletePage(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&page_id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "deletePage.py", -1, "page_content");

}

function newPage(){

  if(document.getElementById){
    document.getElementById("active_page").innerHTML="Nouvelle page<br><br><ul><li>&nbsp;Titre (page) :&nbsp;<input type='text' size='10' id='page_title' value='page'></li><li>&nbsp;Nom (menu) :&nbsp;<input type='text' size='10' id='nav_name' value='label'></li><li>&nbsp;Titre (menu) :&nbsp;<input type='text' size='10' id='nav_title' value='info bulle'></li><li>&nbsp;Parent (menu) :&nbsp;<input type='text' size='3' id='nav_parent' value='0'></li><li>&nbsp;Position (menu) :&nbsp;<input type='text' size='3' id='nav_position' value='0'></li><li>&nbsp;OnClick (menu) :&nbsp;<input type='text' size='10' id='nav_onclick' value='goPage(this.id)'></li><li>&nbsp;Page par defaut :&nbsp;<input type='text' size='3' id='nav_selected' value='0'></li></ul><br><br>Contenu de la page<br><textarea rows='10' cols='20' id='pg_content'>saisir le contenu de la page ici...</textarea><br><input type='button' onclick='createPage()' value='Enregistrer' title='Sauvegarder la page'><br/><br/><a href='javascript:setup();'><img src='/img/edit_icon.png' height='16' width='16' title='Load TinyMCE'/></a>";
    getFocus("page_title");
  }

}

function createPage(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&page_title="+encodeURIComponent(document.getElementById("page_title").value);
    myValues += "&nav_name="+encodeURIComponent(document.getElementById("nav_name").value);
    myValues += "&nav_title="+encodeURIComponent(document.getElementById("nav_title").value);
    myValues += "&nav_parent_id="+document.getElementById("nav_parent").value;
    myValues += "&nav_position="+document.getElementById("nav_position").value;
    myValues += "&nav_onclick="+encodeURIComponent(document.getElementById("nav_onclick").value);
    myValues += "&nav_selected="+document.getElementById("nav_selected").value;
    myValues += "&page_content="+encodeURIComponent(document.getElementById("pg_content").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "createPage.py", -1, "page_content");

}

function configuration(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetConfig.py", -1, "page_content");

}

function modifyConfig(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&config_id="+document.getElementById("config_id").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetConfig.py", -1, "page_content");

}

function updateConfig(){

  if(document.getElementById){
    if(document.getElementById("title").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un Titre !</b>";
      getFocus("title");
      return;
    }
    if(document.getElementById("url").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir l'url du site !</b>";
      getFocus("url");
      return;
    }
    if(document.getElementById("email").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une adresse email !</b>";
      getFocus("email");
      return;
    }
    if(document.getElementById("language").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir la langue du site !</b>";
      getFocus("language");
      return;
    }
    if(document.getElementById("skin").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir le nom de la skin !</b>";
      getFocus("skin");
      return;
    }
    if(document.getElementById("headtag").value == ""){
      document.getElementById("headtag").value=" ";
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+document.getElementById("config_id").value;
    myValues += "&title="+document.getElementById("title").value;
    myValues += "&pub="+encodeURIComponent(document.getElementById("pub").value);
    myValues += "&description="+encodeURIComponent(document.getElementById("description").value);
    myValues += "&keyword="+encodeURIComponent(document.getElementById("keyword").value);
    myValues += "&url="+encodeURIComponent(document.getElementById("url").value);
    myValues += "&email="+encodeURIComponent(document.getElementById("email").value);
    myValues += "&language="+document.getElementById("language").value;
    myValues += "&skin="+document.getElementById("skin").value;
    myValues += "&headtag="+encodeURIComponent(document.getElementById("headtag").value);
    myValues += "&name="+encodeURIComponent(document.getElementById("name").value);
    if ( document.getElementById("selected_id").checked ){
      myValues += "&selected_id=1";
    }
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updateConfig.py", -1, "page_content");

}

function newConfig(){

  if(document.getElementById){
    document.getElementById("active_page").innerHTML="<br/><h3>Nouvelle Configuration &nbsp;&nbsp;<a href='#' onclick='newConfig()' ><img src='/img/add_icon.gif' title='Nouvelle configuration' width='16' height='16' ></a></h3><br/><div id='msg_box'>&nbsp;</div><table border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='adminTitle'>Titre du site :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='30' id='title'></td></tr><tr><td class='adminTitle'>Slogan Pub :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='100' id='pub'></td></tr><tr><td class='adminTitle'>Description (meta) :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='250' id='description'></td></tr><tr><td class='adminTitle'>Mots clef (meta) :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='250' id='keyword'></td></tr><tr><td class='adminTitle'>URL du site :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='150' id='url' value='http://www.monsite.org/'></td></tr><tr><td class='adminTitle'>Email de contact :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='150' id='email'></td></tr><tr><td class='adminTitle'>Texte bas de page :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='30' id='name'></td></tr><tr><td class='adminTitle'>Langue :</td></tr><tr><td class='adminField'><input type='text' size='3'  maxlength='2' id='language' value='fr'></td></tr><tr><td class='adminTitle'>Nom de la skin :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='30' id='skin' value='default'></td></tr><tr><td class='adminTitle'>Tag head supl&eacute;mentaire :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='250' id='headtag'></td></tr></table><input type='button' onclick='createConfig()' value='Enregistrer' title='Sauvegarder la configuration'>";
    getFocus("title");
  }

}

function createConfig(){

  if(document.getElementById){
    if(document.getElementById("title").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un Titre !</b>";
      getFocus("title");
      return;
    }
    if(document.getElementById("url").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir l'url du site !</b>";
      getFocus("url");
      return;
    }
    if(document.getElementById("email").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une adresse email !</b>";
      getFocus("email");
      return;
    }
    if(document.getElementById("language").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir la langue du site !</b>";
      getFocus("language");
      return;
    }
    if(document.getElementById("skin").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir le nom de la skin !</b>";
      getFocus("skin");
      return;
    }
    if(document.getElementById("headtag").value == ""){
      document.getElementById("headtag").value=" ";
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&title="+document.getElementById("title").value;
    myValues += "&pub="+encodeURIComponent(document.getElementById("pub").value);
    myValues += "&description="+encodeURIComponent(document.getElementById("description").value);
    myValues += "&keyword="+encodeURIComponent(document.getElementById("keyword").value);
    myValues += "&url="+encodeURIComponent(document.getElementById("url").value);
    myValues += "&email="+encodeURIComponent(document.getElementById("email").value);
    myValues += "&language="+document.getElementById("language").value;
    myValues += "&skin="+document.getElementById("skin").value;
    myValues += "&headtag="+encodeURIComponent(document.getElementById("headtag").value);
    myValues += "&name="+encodeURIComponent(document.getElementById("name").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "createConfig.py", -1, "page_content");

}

function deleteConfig(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+document.getElementById("config_id").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "deleteConfig.py", -1, "page_content");

}

function listProduct(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getProductsList.py", -1, "page_content");

}

function modifyProduct(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&product_id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetProduct.py", id, "page_content");

}

function updateProduct(){

  if(document.getElementById){
    if(document.getElementById("reference").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une R&eacute;f&eacute;rence !</b>";
      getFocus("reference");
      return;
    }
    if(document.getElementById("designation").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une D&eacute;signation !</b>";
      getFocus("designation");
      return;
    }
    if(document.getElementById("description").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une Description !</b>";
      getFocus("description");
      return;
    }
    if(document.getElementById("description").value.length > 250){
      var l=document.getElementById("description").value.length
      document.getElementById("msg_box").innerHTML="<b>Description trop longue "+l+" caract. (maxi 250 caract.) !</b>";
      getFocus("description");
      return;
    }
    if(document.getElementById("option").value.length > 250){
      var l=document.getElementById("option").value.length
      document.getElementById("msg_box").innerHTML="<b>Option trop longue "+l+" caract. (maxi 250 caract.) !</b>";
      getFocus("option");
      return;
    }
    if(document.getElementById("price").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un Prix !</b>";
      getFocus("price");
      return;
    }
    if(document.getElementById("tax").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une Taxe !</b>";
      getFocus("tax");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+document.getElementById("product_id").value;
    myValues += "&reference="+encodeURIComponent(document.getElementById("reference").value);
    myValues += "&designation="+encodeURIComponent(document.getElementById("designation").value);
    myValues += "&description="+encodeURIComponent(document.getElementById("description").value);
    myValues += "&price="+encodeURIComponent(document.getElementById("price").value);
    myValues += "&tax="+encodeURIComponent(document.getElementById("tax").value);
    myValues += "&dispo="+encodeURIComponent(document.getElementById("dispo").value);
    myValues += "&option="+encodeURIComponent(document.getElementById("option").value);
    myValues += "&image="+encodeURIComponent(document.getElementById("image").value);
    myValues += "&stock="+encodeURIComponent(document.getElementById("stock").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updateProduct.py", -1, "page_content");

}

function newProduct(){

  if(document.getElementById){
    document.getElementById("active_page").innerHTML="<br/><h3>Nouveau Produit ou Pr&eacute;station &nbsp;&nbsp;<a href='#' onclick='newProduct()' ><img src='/img/add_icon.gif' title='Nouveau Produit' width='16' height='16' ></a></h3><br/><div id='msg_box'>&nbsp;</div><table border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='adminTitle'>R&eacute;f&eacute;rence :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='20' id='reference'></td></tr><tr><td class='adminTitle'>D&eacute;signation :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='100' id='designation'></td></tr><tr><td class='adminTitle'>Description :</td></tr><tr><td class='adminField'><textarea rows='8' cols='24' id='description'></textarea></td></tr><tr><td class='adminTitle'>Options :</td></tr><tr><td class='adminField'><textarea rows='6' cols='24' id='option'></textarea></td></tr><tr><td class='adminTitle'>Image (nom.jpg) :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='150' id='image'></td></tr><tr><td class='adminTitle'>Prix (HT):</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='13' id='price'></td></tr><tr><td class='adminTitle'>Taxe (%):</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='13' id='tax'></td></tr><tr><td class='adminTitle'>Disponible :</td></tr><tr><td class='adminField'><select id='dispo'><option value='oui' selected>Oui</option><option value='non'>Non</option></select></td></tr><tr><td class='adminTitle'>Quantit&eacute; en stock :</td></tr><tr><td class='adminField'><input type='text' size='24' maxlength='13' id='stock'></td></tr></table><input type='button' onclick='createProduct()' value='Enregistrer' title='Sauvegarder le produit'>";
    getFocus("reference");
  }

}

function createProduct(){

  if(document.getElementById){
    if(document.getElementById("reference").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une R&eacute;f&eacute;rence !</b>";
      getFocus("reference");
      return;
    }
    if(document.getElementById("designation").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une D&eacute;signation !</b>";
      getFocus("designation");
      return;
    }
    if(document.getElementById("description").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une Description !</b>";
      getFocus("description");
      return;
    }
    if(document.getElementById("description").value.length > 250){
      var l=document.getElementById("description").value.length
      document.getElementById("msg_box").innerHTML="<b>Description trop longue "+l+" caract. (maxi 250 caract.) !</b>";
      getFocus("description");
      return;
    }
    if(document.getElementById("option").value.length > 250){
      var l=document.getElementById("option").value.length
      document.getElementById("msg_box").innerHTML="<b>Option trop longue "+l+" caract. (maxi 250 caract.) !</b>";
      getFocus("option");
      return;
    }
    if(document.getElementById("price").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un Prix !</b>";
      getFocus("price");
      return;
    }
    if(document.getElementById("tax").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une Taxe !</b>";
      getFocus("tax");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&reference="+encodeURIComponent(document.getElementById("reference").value);
    myValues += "&designation="+encodeURIComponent(document.getElementById("designation").value);
    myValues += "&description="+encodeURIComponent(document.getElementById("description").value);
    myValues += "&price="+encodeURIComponent(document.getElementById("price").value);
    myValues += "&tax="+encodeURIComponent(document.getElementById("tax").value);
    myValues += "&dispo="+encodeURIComponent(document.getElementById("dispo").value);
    myValues += "&option="+encodeURIComponent(document.getElementById("option").value);
    myValues += "&image="+encodeURIComponent(document.getElementById("image").value);
    myValues += "&stock="+encodeURIComponent(document.getElementById("stock").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "createProduct.py", -1, "page_content");

}

function deleteProduct(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "deleteProduct.py", -1, "page_content");

}

function listImg(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetImgList.py", -1, "page_content");

}

function listUser(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getUsersList.py", -1, "page_content");

}

function newUser(){

  if(document.getElementById){
    document.getElementById("active_page").innerHTML="<br/><br/><table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td colspan='2'>LOGIN</td></tr><tr><td colspan='2'><div id='msg_box'>&nbsp;</div></td></tr><tr><td>Nom d'utilisateur :</td><td><input type='text' id='username'></td></tr><tr><td>Mot de passe :</td><td><input type='password' id='passwd'></td></tr><tr><td colspan='2'><input type='button' onclick='createUser()' value='Enregistrer'></td></tr></table><br/><br/>";
    getFocus("username");
  }

}

function createUser(){

  if(document.getElementById){
    if(document.getElementById("username").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un nom d'utilisateur !</b>";
      getFocus("username");
      return;
    }
    if(document.getElementById("passwd").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un mot de passe !</b>";
      getFocus("passwd");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&username="+document.getElementById("username").value;
    myValues += "&passwd="+document.getElementById("passwd").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "createUser.py", -1, "page_content");

}

function modifyUser(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetUser.py", -1, "page_content");

}

function updateUser(){

  if(document.getElementById){
    if(document.getElementById("username").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un nom d'utilisateur !</b>";
      getFocus("username");
      return;
    }
    if(document.getElementById("passwd").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir un mot de passe !</b>";
      getFocus("passwd");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&username="+document.getElementById("username").value;
    myValues += "&passwd="+document.getElementById("passwd").value;
    myValues += "&id="+document.getElementById("user_id").value;
    if ( document.getElementById("admin_id").checked ){
      myValues += "&admin_id=1";
    }
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updateUser.py", -1, "page_content");

}

function deleteUser(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "deleteUser.py", -1, "page_content");

}

function editCSS(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getCSS.py", -1, "page_content");

}

function updateCSS(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&content="+encodeURIComponent(document.getElementById("css_content").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updateCSS.py", -1, "page_content");

}

function enterStore(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getProductsPage.py", -1, "area_store");
  changeVisibility("enter_store")

}

function detailsProduct(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getDetailsProduct.py", -1, "detail_product");

}

function listDirectories(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getDirList.py", -1, "page_content");

}

function documentsList(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&directory="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getDocumentsList.py", -1, "active_page");

}

function deleteDocument(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&file_name="+id;
    myValues += "&directory="+document.getElementsByName("directory")[0].value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getDeleteDocument.py", -1, "active_page");

}

function backupData(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminBackupDB.py", -1, "active_page");

}

function restoreData(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminRestoreDB.py", -1, "active_page");

}

function sqlFiles(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getSQLFiles.py", -1, "active_page");

}

function helpList(){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getHelpList.py", -1, "active_page");

}

function newHelp(){

  if(document.getElementById){
    document.getElementById("active_page").innerHTML="<br/><br/><table border='0' cellspacing='0' cellpadding='0' align='center'><tr><td>Bulle d'aide</td></tr><tr><td><div id='msg_box'>&nbsp;</div></td></tr><tr><td><textarea cols='23' rows='8' id='help_content'></textarea></td></tr><tr><td><input type='button' onclick='createHelp()' value='Enregistrer'></td></tr></table><br/><br/>";
    getFocus("help_content");
  }

}

function createHelp(){

  if(document.getElementById){
    if(document.getElementById("help_content").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une aide !</b>";
      getFocus("help_content");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&help_content="+encodeURIComponent(document.getElementById("help_content").value);
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "createHelp.py", -1, "active_page");

}

function modifyHelp(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "adminGetHelp.py", -1, "active_page");

}

function updateHelp(){

  if(document.getElementById){
    if(document.getElementById("help_content").value == ""){
      document.getElementById("msg_box").innerHTML="<b>Saisir une aide !</b>";
      getFocus("help_content");
      return;
    }
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&help_content="+encodeURIComponent(document.getElementById("help_content").value);
    myValues += "&id="+document.getElementById("help_id").value;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "updateHelp.py", -1, "active_page");

}

function deleteHelp(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "deleteHelp.py", -1, "active_page");

}

function getInfoBull(id){

  if(document.getElementById){
    var myValues = "session="+document.getElementById("session").value;
    myValues += "&id="+id;
  }else{
    return;
  }
  sendXmlHttpRequest(myValues, "getHelp.py", -1, "ib_"+id);

}

function getFocus(id){

  if(document.getElementById){
    document.getElementById(id).focus();
  }

}

function changeVisibility(id){

  if(document.getElementById){
    if(document.getElementById(id).style.display == "none"){
      document.getElementById(id).style.display = "block";
    }else{
      document.getElementById(id).style.display = "none";
    }
  }

}

function startUpload(){

  if(document.getElementById){
      //BUG document.getElementById('upload_process').style.visibility = 'visible';
    document.getElementById('upload_process').style.visibility = 'hidden';
  }

}

function stopUpload(success){

  if(document.getElementById){
    document.getElementById('upload_process').style.visibility = 'hidden';
    if (success == 1){
      document.getElementById('upload_result').innerHTML = 'Upload success';
    }else {
      document.getElementById('upload_result').innerHTML = 'Upload error';
    }
  }
  
}

function setup() {

  tinyMCE.init({
    mode : "textareas",
    theme : "advanced",
    plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
    theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
    theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true
  });

}